'
'   XC Engine (project Minotaur)
'   Copyright (C) 2009 Stanislav Hordiyenko
'
'   This program is free software; you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation; either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program; if not, see http://www.gnu.org/licenses
'   or write to the Free Software Foundation,Inc., 51 Franklin Street,
'   Fifth Floor, Boston, MA 02110-1301 USA
'

Class Environment
	Private objFso
	Private objXmlDocument
	Private objEnvironmentVariables
	Public strEnvironmentName

	Private Sub Class_Initialize()
		Set objFso = CreateObject("Scripting.FileSystemObject")
		Set objEnvironmentVariables = CreateObject("Scripting.Dictionary")
		Set objXmlDocument = CreateObject("Msxml2.DOMDocument")
	End Sub

	Private Sub Class_Terminate()
		Set objFso = Nothing
		Set objEnvironmentVariables = Nothing
		Set objXmlDocument = Nothing
	End Sub
	
	Public Property Get Name
		Name = strEnvironmentName
	End Property
	
	Public Sub Load(ByVal strFileName)
		Set objSys = New Sys
	
		strFileName = strFileName & ".xml"
	
		strPathToEnvironment = objSys.GetPathToFileByFileName(GLOBAL_APPLICATION_PATH & "test artefacts\environments\", strFileName)
		
		If strPathToEnvironment = "" Then
			Err.Raise 5000, "Environment->Load", "The file with environment configuration doesn't exist"
		End If

		If Not objXmlDocument.Load(strPathToEnvironment) Then
			Err.Raise 5000, "Environment", "The file with environment configuration contains corrupted data"
		End If

		blnError = False
		
		On Error Resume Next
			strEnvironmentName = CStr(objXmlDocument.GetElementsByTagName("Name")(0).FirstChild.NodeValue)
		
			Set colVariables = objXmlDocument.GetElementsByTagName("Parameter")

			For Each objVariable In colVariables
				strName = objVariable.GetAttribute("Key")
				strValue = objVariable.GetAttribute("Value")
				
				strValue = Replace(strValue, "\\", "\")
			
				objEnvironmentVariables.Add strName, strValue
			Next
		
			If Err.Number <> 0 Then blnError = True
		On Error Goto 0

		If blnError Then
			Err.Raise 5000, "Environment", "The file with environment configuration contains corrupted data"
		End If
	End Sub
	
	Private strEnvironmentParameter
	
	Public Default Function Item(ByVal strValue)
		strEnvironmentParameter = strValue
		
		Set Item = Me
	End Function
	
	Public Property Get Value
		If strEnvironmentParameter = Empty Then
			Err.Raise 5000, "Environment", "No such parameter for this environment"
		End If
	
		If Not objEnvironmentVariables.Exists(strEnvironmentParameter) Then
			Err.Raise 5000, "Environment", "No such parameter for this environment"
		End If
		
		Value = objEnvironmentVariables(strEnvironmentParameter)
	End Property
End Class